import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import BaiduMap from 'vue-baidu-map';
import axios from "axios";
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
import 'font-awesome/css/font-awesome.min.css'
import routes from './routes'

// 引入
import xxx from "./common/js/permission"

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

// Vue.use(BaiduMap, {
//   ak: 'yPaaAo6Oomp15h5piSsYRu6EkHKLgaUd'
// });

// 引入axios
// axios的基础路径
//axios.defaults.baseURL='/api';
axios.defaults.baseURL="http://localhost:8080/";
// 全局axios属性
Vue.prototype.$http=axios;






const router = new VueRouter({
  routes
})

// =========================================axios的前置拦截器=====================================//
// 发送axios请求之前都会执行以下代码
// 前置拦截:携带token
axios.interceptors.request.use(res=>{
  //从localStorage获取token
  let token =  localStorage.getItem("token");
  if(token){ // 如果token有数据
    res.headers["token"] = token;  //将token设置到请求头
  }
  return res;  // 返回
},error => {
  Promise.reject(error);  // 出错就拒绝
})
// =========================================axios的前置拦截器=====================================//



// ==========================axios的后置拦截器=================//
// 发送axios请求之后都会执行以下代码
axios.interceptors.response.use(res=>{ // res = "{"success":false,"message":"noLogin"}"
  // 后置拦截: 跳转登录页面
  if (!res.data.success && res.data.message == 'noLogin'){  // 判断后端响应是没有登录成功且返回信息为noLogin
    // 登录失败就删除掉localStorage存储的token,logininfo
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
      // 跳转到登录页面
    router.push({path:'/login'});
  }else if(!res.data.success && res.data.message == 'noPermission'){
      alert("你没有该操作权限! 你不配!!!")
  }else if(!res.data.success && res.data.message == 'timeout'){
    // 过期了就删除存储的token,logininfo,permissions,menus
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
    alert("jwt过期了");
  }
  return res;  // 返回
},error => {
  Promise.reject(error);  // 出错就拒绝
})
// ======================axios的后置拦截器===================//


// =========================================路由的前置拦截器=====================================//
// 拦截不经过axios请求的请求
// 发送路由的每一个请求之前
router.beforeEach((to, from, next) => {
  if (to.path == '/login' || to.path == "/register") { // 如果访问的是登录页面 login
    next(); // 访问登录页面login或者注册页面register直接放行
  }else {
    let logininfo = JSON.parse(localStorage.getItem('logininfo')); // 如果访问的不是登录页面,就先获取logininfo,转成json字符串
    if (logininfo){
      next() // 有信息就直接放行
    }else {
      // 就跳转到登录页面
      next({ path: '/login' })
    }
  }
})

//处理页面刷新动态路由失效问题
initIndexRouters();
function initIndexRouters(){
  if(!localStorage.menus){
    return;
  }
  //防止重复配置路由：5就是main.js中路由的个数 - 如果你的静态路由是6个这里要写成6
  if(router.options.routes.length>5){
    return;
  }
  let menus = localStorage.getItem('menus');
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  //动态路由配置
  router.addRoutes(tempRouters);
}


//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

